package dev;
/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2013 Jean-Sebastien Lerat (Jean-Sebastien.Lerat@ulb.ac.be)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * 
 * @author Jean-Sebastien Lerat (Jean-Sebastien.Lerat@ulb.ac.be)
 * @version 1.00, 24/06/2013
 */

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Arrays;

import junit.framework.Assert;

import org.junit.Test;

import be.ac.ulb.mlg.utils.Measure;
import be.ac.ulb.mlg.utils.Measurer;

public abstract class MeasureTestCase{

	private final double[] correlated;
	private final double[] uncorrelated;
	private final double[] mixed;
	protected static final int SIZE = 100;
	protected static final boolean[] NO_COMPUTABLE = null;
	private static final double EPS = 1E-10;
	
	private final double correlatedExpected;
	private final double uncorrelatedExpected;
	private final Measure measure;
	
	protected MeasureTestCase(Measure measure){
		correlated		= new double[SIZE];
		uncorrelated	= new double[SIZE];
		mixed			= new double[SIZE];
		
		Arrays.fill(correlated, 1.0);
		Arrays.fill(uncorrelated, 0.0);
		Arrays.fill(mixed, 0, SIZE>>1, 1.0);
		Arrays.fill(mixed, SIZE>>1,SIZE, 0.0);
		for(int i=0; i < SIZE; i++){
			final int rand		= (int)(Math.random()*SIZE);
			final double swap	= mixed[i];
			mixed[i]			= mixed[rand];
			mixed[rand]			= swap;
		}
		
		correlatedExpected		= correlated(correlated,correlated);
		uncorrelatedExpected	= uncorrelated(correlated,uncorrelated);
		
		this.measure			= measure;
	}
	protected abstract double correlated(double[] onesA,double[] onesB);
	protected abstract double uncorrelated(double[] onesA,double[] zerosB);
	
	@Test
	public void testCorrelated(){
		assertEquals(
				this.measure.getClass().getSimpleName()+"[correlated]",
				correlatedExpected,
				measure.measure(this.correlated, this.correlated, NO_COMPUTABLE),
				EPS
		);
	}
	@Test
	public void testUncorrelated() {
		assertEquals(
				this.measure.getClass().getSimpleName()+"[uncorrelated]",
				uncorrelatedExpected,
				measure.measure(this.correlated, this.uncorrelated, NO_COMPUTABLE),
				EPS
		);
	}
	@Test
	public void testMixed() {
		final double result = measure.measure(this.correlated, this.mixed, NO_COMPUTABLE);
		assertTrue(
				this.measure.getClass().getSimpleName()+"[mixed is not between correlated and uncorrelated]",
				between(correlatedExpected,uncorrelatedExpected,result)
		);
	}
	@Test
	public void testNative() {
		if(!Measurer.hasNativeSupport())
			fail("No native support");
		if(!this.measure.hasNativeImplementation())
			Assert.assertTrue(true);
		
		final Measurer measurer = new Measurer();
		measurer.useNative(true);
		measurer.setNumberOfThreads(1);
		measurer.setHandelingMissingValues(false);
		
		final double[][] data	= {this.correlated,this.correlated,this.uncorrelated,this.mixed};
		final double[][] result = measurer.measure(data,new Measure[]{this.measure}).values().iterator().next();

		
		assertEquals(
				this.measure.getClass().getSimpleName()+"[native Correlated]",
				this.correlatedExpected,
				result[0][0],
				EPS
		);
		
		assertEquals(
				this.measure.getClass().getSimpleName()+"[native Uncorrelated]",
				this.uncorrelatedExpected,
				result[0][1],
				EPS
		);
		
		assertEquals(
				this.measure.getClass().getSimpleName()+"[native Mixed]",
				measure.measure(this.correlated, this.mixed, NO_COMPUTABLE),
				result[0][2],
				EPS
		);
				
	}
	private static final boolean between(final double leftExclusive,final double rightExclusive,final double value){
		if(rightExclusive < leftExclusive)return between(rightExclusive,leftExclusive,value);
		return value > leftExclusive && value < rightExclusive;
	}
}
